package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.mapper.RoleMapper;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public void save(Role role, Long[] ids) {

        roleMapper.insert(role);
        //往中间表添加数据
        if(ids != null && ids.length>0){
            for (Long id : ids) {
                roleMapper.insertByPermissionId(role.getId(),id);
            }
        }
    }

    @Override
    public void delete(Long id) {

        roleMapper.deleteByPrimaryKey(id);
        //删除中间表的数据
        roleMapper.deleteByRoleId(id);
    }

    @Override
    public void update(Role role, Long[] ids) {

        roleMapper.updateByPrimaryKey(role);
        //删除之前的数据
        roleMapper.deleteByRoleId(role.getId());
        //往中间表更新数据
        if(ids != null && ids.length>0){
            for (Long id : ids) {
                roleMapper.insertByPermissionId(role.getId(),id);
            }
        }
    }

    @Override
    public Role get(Long id) {

        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Role> list() {
        return roleMapper.selectAll();
    }

    @Override
    public PageInfo query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Role> list = roleMapper.selectForList(qo);
        return new PageInfo(list);
    }
}
